package shan;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.lang.reflect.Array;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;


public class Hdfs {
    FileSystem fs = null;
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        Configuration configuration = new Configuration();
        configuration.set("dfs,replication","3");
        configuration.set("dfs,blocksize","128m");
        fs = FileSystem.get(new URI("hdfs://192.168.88.5:9000"),configuration,"root");
    }
    /**
     * 从本地磁盘上传到HDFS
     */
    @Test
    public void copyHdfs() throws IOException {
        FileInputStream in = new FileInputStream(new File("f:\\jian.mp4"));
        OutputStream out = fs.create(new Path("/video/jian.mp4"));
        IOUtils.copyBytes(in,out,4096,true);
        System.out.println("拷贝完成。。。");
        fs.close();
    }
    /**
     * 从HDFS下载文件到本地磁盘
     */
    @Test
    public void  testGet() throws IOException {
        fs.copyToLocalFile(false,new Path("/music/jing.mp3"),new Path("e:\\"),true);
        fs.close();
    }
    /**
     * 在HDFS移动文件/修改名称
     */
    @Test
    public void testRename() throws IOException {
        fs.rename(new Path("/document/a.txt"),new Path("/document/linyujie.txt"));
        fs.close();
    }
    /**
     * 在HDFS创建文件
     */
    @Test
    public void testmkdir() throws IOException {
        fs.mkdirs(new Path("/linyujie"));
        fs.close();
    }
    /**
     * 在HDFS中删除文件或文件夹
     */
    @Test
    public void testrm() throws IOException {
        fs.delete(new Path("/linyujie"),true);
        fs.close();
    }
    /**
     *查询HDFS指定目录下的文件信息
     */
    @Test
    public void testSee() throws IOException {
        RemoteIterator<LocatedFileStatus> it = fs.listFiles(new Path("/music"),true);
        while(it.hasNext()){
            LocatedFileStatus status = it.next();
            System.out.println("文件全路径"+status.getPath());
            System.out.println("块大小"+status.getBlockSize());
            System.out.println("文件长度"+status.getLen());
            System.out.println("副本数量"+status.getReplication());
            System.out.println("块信息"+ Arrays.toString(status.getBlockLocations()));
            System.out.println("---------------------------------------------");
        }
        fs.close();
    }
    @Test
    public void testseet() throws IOException {
        FileStatus[] liststatus = fs.listStatus(new Path("/video"));
        for(FileStatus status : liststatus){
            System.out.println("文件全路径"+status.getPath());
            System.out.println(status.isDirectory()?"这是文件夹":"这是文件");
            System.out.println("块大小"+status.getBlockSize());
            System.out.println("文件长度"+status.getLen());
            System.out.println("副本数量"+status.getReplication());
            System.out.println("----------------------");
        }
        fs.close();
    }
}
